<template>
  <div class="table-page">
    <GiTable
      title="{{.TableComment}}管理"
      row-key="{{.PkGoField}}"
      :data="dataList"
      :columns="columns"
      :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }"
      :pagination="pagination"
      :disabled-tools="['size']"
      :disabled-column-keys="['name']"
      @refresh="search"
    >
      <template #toolbar-left>
       {{range .Columns}}
       {{- $x := .IsQuery -}}
       {{- if (eq $x true) -}}
       {{- if ne .FkTableName "" -}}
          <a-select v-model="queryForm.{{.JsonField}}" :options="status_enum" placeholder="请选择状态{{.ColumnComment}}" allow-clear style="width: 150px" @change="search" />
       {{- else -}}
       {{if eq .DictType "" -}}
          <a-input v-model="queryForm.{{.JsonField}}" placeholder="请输入{{.ColumnComment}}" allow-clear @change="search">
            <template #prefix><icon-search />
          </template>
       {{- else -}}
           <a-select v-model="queryForm.{{.JsonField}}" :options="status_enum" placeholder="请选择状态{{.ColumnComment}}" allow-clear style="width: 150px" @change="search" />
       {{- end}}
       {{- end}}
       {{end}}
       {{- end }}
        <a-button @click="reset">
          <template #icon><icon-refresh /></template>
          <template #default>重置</template>
        </a-button>
      </template>
      <template #toolbar-right>
        <a-button v-permission="['{{.PackageName}}:{{.ModuleName}}:add']" type="primary" @click="onAdd">
          <template #icon><icon-plus /></template>
          <template #default>新增</template>
        </a-button>
        <a-button v-permission="['{{.PackageName}}:{{.ModuleName}}:export']" @click="onExport">
          <template #icon><icon-download /></template>
          <template #default>导出</template>
        </a-button>
      </template>
      <template #{{.ModuleName}}Scope="{ record }">
        <GiCellTag :value="record.noticeScope" :dict="dis_enable_status_enum" />
      </template>
      <template #action="{ record }">
        <a-space>
          <a-link v-permission="['{{.PackageName}}:{{.ModuleName}}:detail']" title="详情" @click="onDetail(record)">详情</a-link>
          <a-link v-permission="['{{.PackageName}}:{{.ModuleName}}:update']" title="修改" @click="onUpdate(record)">修改</a-link>
          <a-link
            v-permission="['{{.PackageName}}:{{.ModuleName}}:delete']"
            status="danger"
            :disabled="record.disabled"
            :title="record.disabled ? '不可删除' : '删除'"
            @click="onDelete(record)"
          >
            删除
          </a-link>
        </a-space>
      </template>
    </GiTable>

    <{{.ClassName}}AddModal ref="{{.ClassName}}AddModalRef" @save-success="search" />
    <{{.ClassName}}DetailDrawer ref="{{.ClassName}}DetailDrawerRef" />
  </div>
</template>

<script setup lang="ts">
import {{.ClassName}}AddModal from './FormAddModal.vue'
import {{.ClassName}}DetailDrawer from './FormDetailDrawer.vue'
import { type {{.ClassName}}Resp, type {{.ClassName}}Query, delete{{.ClassName}}, export{{.ClassName}}, list{{.ClassName}} } from '@/apis/{{.PackageName}}/{{.ModuleName}}'
import type { TableInstanceColumns } from '@/components/GiTable/type'
import { useDownload, useTable } from '@/hooks'
import { useDict } from '@/hooks/app'
import { isMobile } from '@/utils'
import has from '@/utils/has'

defineOptions({ name: '{{.ClassName}}' })

const { dis_enable_status_enum } = useDict('dis_enable_status_enum')

const queryForm = reactive<{{.ClassName}}Query>({
  {{ range .Columns }}
  {{- if (.IsQuery) -}}
  {{.JsonField}}: undefined,
  {{- end }}
  {{- end }}
  sort: ['id,desc']
})

const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete
} = useTable((page) => list{{.ClassName}}({ ...queryForm, ...page }), { immediate: true })
const columns: TableInstanceColumns[] = [

 {{ range .Columns }}
 {{- if (.IsQuery) -}}
  { title: '{{.ColumnComment}}', dataIndex: '{{.ColumnName}}', slotName: '{{.ColumnName}}' },
 {{- end }}
 {{- end }}
  {
    title: '操作',
    dataIndex: 'action',
    slotName: 'action',
    width: 160,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: has.hasPermOr(['{{.PackageName}}:{{.ModuleName}}:detail', '{{.PackageName}}:{{.ModuleName}}:update', '{{.PackageName}}:{{.ModuleName}}:delete'])
  }
]

// 重置
const reset = () => {
  {{ range .Columns }}
  {{- if (.IsQuery) -}}
  queryForm.{{.JsonField}}= undefined
  {{- end }}
  {{- end }}
  search()
}

// 删除
const onDelete = (record: {{.ClassName}}Resp) => {
  return handleDelete(() => delete{{.ClassName}}(record.id), {
    content: `是否确定删除该条数据？`,
    showModal: true
  })
}

// 导出
const onExport = () => {
  useDownload(() => export{{.ClassName}}(queryForm))
}

const {{.ClassName}}AddModalRef = ref<InstanceType<typeof {{.ClassName}}AddModal>>()
// 新增
const onAdd = () => {
  {{.ClassName}}AddModalRef.value?.onAdd()
}

// 修改
const onUpdate = (record: {{.ClassName}}Resp) => {
  {{.ClassName}}AddModalRef.value?.onUpdate(record.id)
}

const {{.ClassName}}DetailDrawerRef = ref<InstanceType<typeof {{.ClassName}}DetailDrawer>>()
// 详情
const onDetail = (record: {{.ClassName}}Resp) => {
  {{.ClassName}}DetailDrawerRef.value?.onOpen(record.id)
}
</script>

<style scoped lang="scss"></style>
